library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Creating Figure 6.6
# Wave 1

# Create binaries for treatement groups and controls
# Trump Politicizes
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_tp[mil_conf.df$DOV_ASSIGNMENT_C == 3] <- 1
# Dem Politicizes
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_dep[mil_conf.df$DOV_ASSIGNMENT_C == 4] <- 1
# Mil SUpports
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_ms[mil_conf.df$DOV_ASSIGNMENT_C == 5] <- 1
# Mil opposes
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 2] <- 0
mil_conf.df$q42_mo[mil_conf.df$DOV_ASSIGNMENT_C == 6] <- 1

# Code agreement with Trust the military
mil_conf.df$Q42F2[mil_conf.df$Q42F < 77] <- 0
mil_conf.df$Q42F2[mil_conf.df$Q42F < 3] <- 1

# Drop NA
mil_conf.dff <- mil_conf.df[!is.na(mil_conf.df$Q42F2),]

# Creating weighted survey design objects
w1_design_f <-
  svydesign(
    id = ~ 1,
    weights = ~ weight,
    data = mil_conf.dff
  )

# Wave 1
# Trump Politicizes
w1t_d <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==0])
w1t_i <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==1])
w1t_r <- svyttest(Q42F2~q42_tp, w1_design_f[w1_design_f$variables$party==2])

# Dem Politicizes
w1d_d <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==0])
w1d_i <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==1])
w1d_r <- svyttest(Q42F2~q42_dep, w1_design_f[w1_design_f$variables$party==2])

# Mil Supports
w1ms_d <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==0])
w1ms_i <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==1])
w1ms_r <- svyttest(Q42F2~q42_ms, w1_design_f[w1_design_f$variables$party==2])

# Mil Opposes
w1mo_d <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==0])
w1mo_i <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==1])
w1mo_r <- svyttest(Q42F2~q42_mo, w1_design_f[w1_design_f$variables$party==2])



# Wave 1
pt.est1 <- c(w1t_d$estimate, w1t_i$estimate, w1t_r$estimate,
             w1d_d$estimate, w1d_i$estimate, w1d_r$estimate,
             w1ms_d$estimate, w1ms_i$estimate, w1ms_r$estimate,
             w1mo_d$estimate, w1mo_i$estimate, w1mo_r$estimate)
ci.low1 <- c(w1t_d$conf.int[1], w1t_i$conf.int[1], w1t_r$conf.int[1],
             w1d_d$conf.int[1], w1d_i$conf.int[1], w1d_r$conf.int[1],
             w1ms_d$conf.int[1], w1ms_i$conf.int[1], w1ms_r$conf.int[1],
             w1mo_d$conf.int[1], w1mo_i$conf.int[1], w1mo_r$conf.int[1])
ci.high1 <- c(w1t_d$conf.int[2], w1t_i$conf.int[2], w1t_r$conf.int[2],
              w1d_d$conf.int[2], w1d_i$conf.int[2], w1d_r$conf.int[2],
              w1ms_d$conf.int[2], w1ms_i$conf.int[2], w1ms_r$conf.int[2],
              w1mo_d$conf.int[2], w1mo_i$conf.int[2], w1mo_r$conf.int[2])
plot.labs <- c(" ","Trump\nPoliticizes","  ",
               "   ","Democrat\nPoliticizes","    ",
               "     ","Military\nSupports", "      ",
               "       ","Military\nOpposes", "        ")
df.plot1 <- data.frame(Treatment=plot.labs, estimate=pt.est1, 
                       lower=ci.low1, upper=ci.high1)
df.plot1$Treatment <- factor(df.plot1$Treatment, levels = plot.labs)

plot_w1 <- ggplot()+
  geom_pointrange(data=df.plot1, 
                  mapping = aes(x=Treatment,
                                y=estimate,
                                ymin=lower,
                                ymax=upper),
                  size = 0.5, color = c('blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red',
                                        'blue','gray50','red'))+
  geom_hline(yintercept = 0, color = "black")+
  ylim(-.25,.3)+
  ylab("Difference in Means from Control Condition")+
  xlab("Treatment Condition")+
  theme_bw()

